<%@page import="com.tahado.service.SignedRequestParser"%>
<%@ page import="java.util.*" %>
<%@ page import="net.sf.json.JSONObject" %>

<%@ page import ="java.net.URLEncoder" %>
<%@ page import ="com.restfb.*" %>
 
<%@ page import ="com.restfb.types.*" %>
 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set     -->
<!-- the browser's rendering engine into           -->
<!-- "Quirks Mode". Replacing this declaration     -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout.   -->

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Tahado</title>
  </head>

  <body>
  
  <% 

 

 //SignedRequest sr = SignedRequest.parseSignedRequest(request.getParameter("signed_request"), SignedRequestParser.fbSecretKey);
  JSONObject data = SignedRequestParser.parse(request.getParameter("signed_request"));
 out.println(data.toString(1));
 
 
 



 //check if user authorized the app
 if(!data.has("user_id") || !data.has("oauth_token")) {
     //this is guest, create  that will be passed to javascript
     //note that redirect_uri (page the user will be forwarded to after authorization) is set to fbCanvasUrl
//     mav.addObject("redirectUrl", "https://www.facebook.com/dialog/oauth?client_id=" + fbAppId + 
//             "&redirect_uri=" + URLEncoder.encode(fbCanvasUrl, "UTF-8") + 
//             "&scope=publish_stream,offline_access,email");
 	String authorizationUrl ="https://www.facebook.com/dialog/oauth?client_id=" + SignedRequestParser.fbAppId + 
   "&redirect_uri=" + URLEncoder.encode(SignedRequestParser.fbCanvasPage, "UTF-8") + 
   "&scope=publish_stream,offline_access,email&response_type=token";
 	
 	
 	authorizationUrl = "http://www.facebook.com/dialog/oauth/?scope=email,user_birthday&client_id="+SignedRequestParser.fbAppId+"&redirect_uri="+SignedRequestParser.fbCanvasUrl+"&response_type=token";
 	
 	response.sendRedirect(authorizationUrl);
 } else {
     //this is authorized user, get their info from Graph API using received access token
     String accessToken = data.getString("oauth_token");
     FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
     User user = facebookClient.fetchObject("me", User.class);
     
     Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
     
     for (List<User> list: myFriends){

     for (User friend : list)
     {
    	 out.print("<br/>Name :"+friend.getName() + "<img src='http://graph.facebook.com/"+friend.getId()+"/picture'/>");
    	 
    	 
     }}

 }


%>
   
	
    <table>
      <tr>
        <td colspan="2" style="font-weight:bold;">Available Servlets:</td>        
      </tr>
      <tr>
        <td><a href="tahado6">Tahado6</a></td>
        <td><a href="http://www.facebook.com/dialog/oauth/?scope=email,user_birthday&client_id=114695075294742&redirect_uri=http://localhost:8888/tahado6&response_type=token">Allow</a></td>



      </tr>
    </table>
  </body>
</html>
